package com.april.controller;

import com.april.feign.ItemFeign;
import com.april.pojo.TbItem;
import com.april.utils.PageResult;
import com.april.utils.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Author: 槐序 廿一
 * @Date: 2023-06-19
 * @Time: 19:18
 * @Description:
 */
@RestController
@RequestMapping("backend")
public class ItemWebController {

    @Resource
    ItemFeign itemFeign;

    @RequestMapping("item/selectTbItemAllByPage")
    public Result selectTbItemAllByPage(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                        @RequestParam(value = "rows", required = false, defaultValue = "10") int rows) {
        PageResult pageResult = itemFeign.selectTbItemAllByPage(page, rows);
        if (pageResult == null) {
            return Result.error("分页查询商品信息失败");
        }
        return Result.ok(pageResult);
    }

    @RequestMapping("item/insertTbItem")
    public Result insertTbItem(TbItem tbItem, String desc, String itemParams) {
        int num = itemFeign.insertTbItem(tbItem, desc, itemParams);
        if (num != 3) {
            return Result.error("添加失败");
        }
        return Result.ok(num);
    }

    @RequestMapping("item/preUpdateItem")
    public Result preUpdateItem(@RequestParam(value = "itemId") Long itemId) {
        Map<String, Object> map = itemFeign.preUpdateItem(itemId);
        if (map == null) {
            return Result.error("信息回显失败");
        }
        return Result.ok(map);
    }

    @RequestMapping("item/updateTbItem")
    public Result updateTbItem(TbItem tbItem, String desc, String itemParams) {
        int num = itemFeign.updateTbItem(tbItem, desc, itemParams);
        if (num != 3) {
            return Result.error("商品更新失败");
        }
        return Result.ok(num);
    }
}
